﻿@{
    ViewBag.Title = ViewData["Action"].ToString() + " Schedule - Project Management System";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@ViewData["Action"].ToString() Schedule</h2>
<form id="form" action="" method="post">
<div class="content-box" id="Schedule">
    <!-- From Content Box -->
    <div class="content-box-header">
        <h3>
            Schedule Info</h3>
        <div class="clear">
        </div>
    </div>
    <!-- To .content-box-header -->
    <div class="content-box-content">
        @{ if (TempData["Message"] != null)
           {
            <div class="notification png_bg" id="messageBox">
                <a href="#" class="close">
                    <img src="../../Content/images/icons/cross_grey_small.png" title="Close this notification"
                        alt="close" /></a>
                <div>
                    @TempData["Message"].ToString()
                </div>
            </div>
           }
        }
        <fieldset>
            <p>
                <input type="hidden" id="Action" name="Action" value="@ViewData["Action"]" />
                <input type="hidden" id="txtScheduleID" name="ScheduleID" value="@Model.ScheduleID" />
                <input type="hidden" id="txtStatus" name="Status" value="@Model.Status" />
                <input type="hidden" id="txtOwnerID" name="OwnerID" value="@Model.OwnerID" />
                <input type="hidden" id="txtStartDate" name="StartDate" value="@Model.StartDate.ToString("yyyy/MM/dd")" />
                <input type="hidden" id="txtEndDate" name="EndDate" value="@Model.EndDate.ToString("yyyy/MM/dd")" />
                <label class="width12 floatLeft">
                    Schedule Name:</label>
                <input type="text" id="txtScheduleName" name="ScheduleName" class="width20 input-inline text-input medium-input" value="@Model.ScheduleName"/>
            </p>
            <p>
                <label class="width12 floatLeft">
                    Project:</label>
                @Html.DropDownList("ProjectID", (SelectList)ViewData["ProjectList"], new { @class = "width20 input-inline" })
            </p>
             <p>
                <label class="width12 floatLeft">
                    Semester:</label>
                @Html.DropDownList("SemesterID", (SelectList)ViewData["SemesterList"], new { @class = "width20 input-inline" })
            </p>
            <p>
                <label class="width12 floatLeft">
                    Description:</label>
                <input type="text" id="txtDescription" name="Description" class="width20 input-inline text-input medium-input" value="@Model.Description"/>
            </p>
            <br clear="all" />
            <h3>
                Tasks</h3>
            <br clear="all" />
            <div id="Timelines">
                @{if (ViewData["Action"].ToString() == PMS.ENT.ActionName.Insert.ToString())
                  {
                    <fieldset id="Timelines1">
                        <h4>
                            Task 1</h4>
                        <div id="Timeline1">
                            <div class="childCol">
                                <p>
                                    <label class="width12 floatLeft">
                                        From Date:</label>
                                    <input type="text" id="txtFromDate1" class="DateTime width20 input-inline text-input" />
                                    <input type="hidden" id="htxtFromDate1" name="ListScheduleItem[0].FromDate" />
                                </p>
                            </div>
                            <div class="childCol">
                                <p>
                                    <label class="width12 floatLeft">
                                        To Date:</label>
                                    <input type="text" id="txtToDate1" class="DateTime width20 input-inline text-input " />
                                    <input type="hidden" id="htxtToDate1" name="ListScheduleItem[0].ToDate" />
                                </p>
                            </div>
                            <br clear="all" />
                            <p>
                                <label class="width12 floatLeft">
                                    Work:</label>
                                <input type="text" id="txtWork1" name="ListScheduleItem[0].Work" class="width20 input-inline text-input medium-input" />
                            </p>
                            <p>
                                <label class="width12 floatLeft">
                                    Percent:</label>
                                <input type="text" id="txtPercent1" name="ListScheduleItem[0].Percent" class=" width20 input-inline text-input" />
                            </p>
                            <p>
                                <label class="width12 floatLeft">
                                    Description:</label>
                                <input type="text" id="txtDescription1" name="ListScheduleItem[0].Description" class="width20 input-inline text-input medium-input" />
                            </p>
                            <p>
                                <a href="#" onclick="RemoveItem('Timelines1')">
                                    <img src="../../Content/images/cancel_48.gif" alt="" />
                                    Remove this task</a>
                            </p>
                        </div>
                    </fieldset> 
                  }
                  else
                  {
                      for (int count = 0; count < Model.ListScheduleItem.Count; count++)
                      {
                    <fieldset id="Timelines@(count)">
                        <h4>
                            Task @(count + 1)</h4>
                        <div id="Timeline@(count)">
                            <div class="childCol">
                                <input type="hidden" id="txtScheduleItemID@(count)" name="ListScheduleItem[@count].ScheduleItemID" value="@Model.ListScheduleItem[count].ScheduleItemID" />
                                <input type="hidden" id="txtScheduleID@(count)" name="ListScheduleItem[@count].ScheduleID" value="@Model.ListScheduleItem[count].ScheduleID"/>
                                <p>
                                    <label class="width12 floatLeft">
                                        From Date:</label>
                                    <input type="text" id="txtFromDate@(count)" class="DateTime width20 input-inline text-input" value="@Model.ListScheduleItem[count].FromDate.ToString("dd/MM/yyyy")"/>
                                    <input type="hidden" id="htxtFromDate@(count)" name="ListScheduleItem[@count].FromDate" value="@Model.ListScheduleItem[count].FromDate.ToString("yyyy/MM/dd")"/>
                                </p>
                            </div>
                            <div class="childCol">
                                <p>
                                    <label class="width12 floatLeft">
                                        To Date:</label>
                                    <input type="text" id="txtToDate@(count)" class="DateTime width20 input-inline text-input " value="@Model.ListScheduleItem[count].ToDate.ToString("dd/MM/yyyy")"/>
                                    <input type="hidden" id="htxtToDate@(count)" name="ListScheduleItem[@count].ToDate" value="@Model.ListScheduleItem[count].FromDate.ToString("yyyy/MM/dd")"/>
                                </p>
                            </div>
                            <br clear="all" />
                            <p>
                                <label class="width12 floatLeft">
                                    Work:</label>
                                <input type="text" id="txtWork@(count)" name="ListScheduleItem[@count].Work" class="width20 input-inline text-input medium-input" value="@Model.ListScheduleItem[count].Work"/>
                            </p>
                            <p>
                                <label class="width12 floatLeft">
                                    Percent:</label>
                                <input type="text" id="txtPercent@(count)" name="ListScheduleItem[@count].Percent" class="DENumber width20 input-inline text-input" value="@Model.ListScheduleItem[count].Percent"/>
                            </p>
                            <p>
                                <label class="width12 floatLeft">
                                    Description:</label>
                                <input type="text" id="txtDescription@(count)" name="ListScheduleItem[@count].Description" class="width20 input-inline text-input medium-input" value="@Model.ListScheduleItem[count].Description"/>
                            </p>
                            @{if (ViewData["Action"].ToString() != PMS.ENT.ActionName.View.ToString())
                              {
                                <p>
                                    <a href="#" onclick="RemoveItem('Timelines@(count)')">
                                        <img src="../../Content/images/cancel_48.gif" alt="" />
                                        Remove this task</a>
                                </p>
                              }
                            }
                        </div>
                    </fieldset> 
                      }
                  }
                }
            </div>
            @{if (ViewData["Action"].ToString() != PMS.ENT.ActionName.View.ToString())
              {
                <p>
                    <a href="#" onclick="AddItem();">
                        <img src="../../Content/images/add_48.gif" />Add new item</a>
                </p>
              }
            }
            <p>
                @{if (ViewData["Action"].ToString() != PMS.ENT.ActionName.View.ToString())
                  {
                    <input class="button" type="submit" value="Submit" id="btnSubmit" />
                  }
                }
                <input class="button" type="button" value="Back to list" id="btnBack" onclick="DoUrl('@Url.Action("ManageSchedule", "Schedule")')" />
                @*<input class="button" type="button" value="View Schedule" id="btnView" />*@
            </p>
        </fieldset>
    </div>
</div>
</form>
<script type="text/javascript">
    $("#MSchedule").addClass("current");
    $("#Schedule").addClass("current");
    var action = document.getElementById("Action").value;
    var fileCount = 0;
    if (action == 'Insert') {
        fileCount = 1;
    }
    else {
        fileCount = @Model.ListScheduleItem.Count;
    }

    var dtSettings;




    $('input.DateTime').live('click', function () {
        $(this).datepicker({
            showOn: 'focus',
            dateFormat: 'dd/mm/yy',
            gotoCurrent: true,
            changeMonth: true,
            changeYear: true,
            showStatus: true,
            showWeeks: true,
            highlightWeek: true,
            numberOfMonths: 1,
            showButtonPanel: true,
            hideIfNoPrevNext: true,
            beforeShow: setMinMaxDate,
            showAnim: "scale",
            showOptions: {
                origin: ["top", "left"]
            }
        }).focus();
        var id = $(this).attr('id');
        var formID = '';
        var toID = '';
        if (id.indexOf('From') >= 0) {
            formID = id;
            toID = id.replace('From', 'To');
        }
        else {
            formID = id.replace('To', 'From');
            toID = id;
        }
        $(".DateTime").filter_inputDateTime(DateTimeFormat);
        CheckDate(formID, toID, true);
    });

    $("#SemesterID").change(function () {
        $(".DateTime").val("");

        var SemesterID = $(this).val();
        $.post('@Url.Action("GetSemester", "Schedule")', { ID: SemesterID }, function (data) {

            if (data != null) {
                $("#txtStartDate").val(data.StartDate);
                $("#txtEndDate").val(data.EndDate);
            }
        });
        $('form').load();
    });

    function setMinMaxDate() {
        if (document.getElementById("txtStartDate").value != '0001/01/01' && document.getElementById("txtEndDate") != '0001/01/01') {
            sDate = document.getElementById("txtStartDate").value.split('/');
            eDate = document.getElementById("txtEndDate").value.split('/');
            fSDate = new Date(sDate[0], sDate[1] - 1, sDate[2]);
            fEDate = new Date(eDate[0], eDate[1] - 1, eDate[2]);
            $(".DateTime").datepicker("option", "minDate", fSDate);
            $(".DateTime").datepicker("option", "maxDate", fEDate);
        }
    }

    $("#ui-datepicker-div").css('display', 'none');

    function AddItem() {
        fileCount = fileCount + 1;
        var html = '<fieldset id="Timelines' + fileCount + '">\
                        <h4>\
                            Task ' + fileCount + '</h4>\
                        <div id="Timeline' + fileCount + '">\
                            <div class="childCol">\
                                <p>\
                                    <label class="width12 floatLeft">\
                                        From Date:</label>\
                                    <input type="text" id="txtFromDate' + fileCount + '" class="DateTime width20 input-inline text-input" />\
                                    <input type="hidden" id="htxtFromDate' + fileCount + '" name="ListScheduleItem[' + (fileCount - 1) + '].FromDate" />\
                                </p>\
                            </div>\
                            <div class="childCol">\
                                <p>\
                                    <label class="width12 floatLeft">\
                                        To Date:</label>\
                                    <input type="text" id="txtToDate' + fileCount + '" class="DateTime width20 input-inline text-input " />\
                                    <input type="hidden" id="htxtToDate' + fileCount + '" name="ListScheduleItem[' + (fileCount - 1) + '].ToDate" />\
                                </p>\
                            </div>\
                            <br clear="all" />\
                            <p>\
                                <label class="width12 floatLeft">\
                                    Work:</label>\
                                <input type="text" id="txtWork' + fileCount + '" name="ListScheduleItem[' + (fileCount - 1) + '].Work" class="width20 input-inline text-input medium-input" />\
                            </p>\
                            <p>\
                                <label class="width12 floatLeft">\
                                    Percent:</label>\
                                <input type="text" id="txtPercent' + fileCount + '" name="ListScheduleItem[' + (fileCount - 1) + '].Percent" class="DENumber width20 input-inline text-input" />\
                            </p>\
                            <p>\
                                <label class="width12 floatLeft">\
                                    Description:</label>\
                                <input type="text" id="txtDescription' + fileCount + '" name="ListScheduleItem[' + (fileCount - 1) + '].Description" class="width20 input-inline text-input medium-input" />\
                            </p>\
                            <p>\
                                <a href="#" val="Timelines' + fileCount + '" onclick=RemoveItem("Timelines' + fileCount + '")> <img src="../../Content/images/cancel_48.gif" alt="" /> Remove this task</a>\
                            </p>\
                        </div>\
                    </fieldset> ';
        $('#Timelines').append(html);
        $("#txtPercent" + fileCount).filter_inputDecimal('.');
    }

    function RemoveItem(id) {
        $('#' + id).remove();
        fileCount = fileCount - 1;

    }


    $("#btnView").click(function () {
        var id = $("#txtScheduleID").val();
        $.post('@Url.Action("ScheduleView", "Schedule")', { ID: id }, function (data) {
            jQuery.facebox(' <h3>\
                                Grantt Chart</h3>\
                                <fieldset>\
                                    <div id="gantt" class="gantt" style="width:800px;"></div>\
                                </fieldset>'
                            );
            
            $("#gantt").gantt({
                source: '../../JsonData/jsonData' + data.ID + '.js',
                navigate: "scroll",
                scale: "days",
                maxScale: "months",
                minScale: "days",
                itemsPerPage: 10
            });
        });
    });


</script>
